package yur.wedding.modules.sys.service.impl;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.transaction.annotation.Transactional;
import yur.wedding.common.utils.Query;

import yur.wedding.modules.sys.dao.SysRoleMenuDao;
import yur.wedding.modules.sys.entity.SysRoleMenuEntity;
import yur.wedding.modules.sys.service.SysRoleMenuService;
import yur.wedding.utils.PageUtils;


@Service("sysRoleMenuService")
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuDao, SysRoleMenuEntity> implements SysRoleMenuService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysRoleMenuEntity> page = this.page(
                new Query<SysRoleMenuEntity>().getPage(params),
                new QueryWrapper<SysRoleMenuEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 添加 角色与菜单的关系
     * @param roleId
     * @param menuIdList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> menuIdList) {
        //因为该方法是添加与修改共用 先删除关系
        deleteBatch(new Long[]{roleId});
        if (menuIdList.size() != 0) {

            for (Long menid : menuIdList) {
                SysRoleMenuEntity roleMenuEntity = new SysRoleMenuEntity();
                roleMenuEntity.setMenuId(menid);
                roleMenuEntity.setRoleId(roleId);
                this.save(roleMenuEntity);
            }
        }
        return;

    }



    private void deleteBatch(Long[] longs) {

        baseMapper.deleteBatch(longs);
    }

    @Override
    public List<Long> queryMenuIdList(Long roleId) {

        return baseMapper.queryMenuIdList(roleId);
    }

}
